package com.autism.app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FirstFragment extends Fragment{
    public static final String TAG = "MAIN";
    private CardView cardView;
    private ImageView imgCover;
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_first, container,false);

//    为卡片设置点击监听事件
//    待优化
    cardView = view.findViewById(R.id.cardView);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),SheYingActivity.class)));
    cardView = view.findViewById(R.id.cardView3);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),KangbaActivity.class)));
    cardView = view.findViewById(R.id.cardView4);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),DidaActivity.class)));
    cardView = view.findViewById(R.id.cardView5);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),WenxueActivity.class)));
    cardView = view.findViewById(R.id.cardView6);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),DouyinActivity.class)));
    cardView = view.findViewById(R.id.cardView2);
    cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),ZhusuActivity.class)));

//    switch (cardView.getId()){
//        case R.id.cardView:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),SheYingActivity.class)));
//            break;
//        case R.id.cardView3:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),KangbaActivity.class)));
//            break;
//        case R.id.cardView4:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),DidaActivity.class)));
//            break;
//        case R.id.cardView5:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),WenxueActivity.class)));
//            break;
//        case R.id.cardView6:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),DouyinActivity.class)));
//            break;
//        case R.id.cardView2:
//            cardView.setOnClickListener(v -> startActivity(new Intent(getActivity(),ZhusuActivity.class)));
//            break;
//    }

//    通过Gilde加载图片
//    待优化
    imgCover = view.findViewById(R.id.imageView3);
    String url0 = "https://cn.yadingtour.com/images/2019_11/20191107181501.jpg";
    Glide.with(this).load(url0).into(imgCover);
    imgCover = view.findViewById(R.id.imageView2);
    String url1 = "https://cn.yadingtour.com/images/2019_11/20191107181502.jpg";
    Glide.with(this).load(url1).into(imgCover);
    imgCover = view.findViewById(R.id.imageView4);
    String url2 = "https://cn.yadingtour.com/images/2019_11/20191107181503.jpg";
    Glide.with(this).load(url2).into(imgCover);
    imgCover = view.findViewById(R.id.imageView5);
    String url3 = "https://cn.yadingtour.com/images/2019_11/20191107181504.jpg";
    Glide.with(this).load(url3).into(imgCover);
    imgCover = view.findViewById(R.id.imageView6);
    String url4 = "https://cn.yadingtour.com/images/2019_11/20191107181505.jpg";
    Glide.with(this).load(url4).into(imgCover);
    imgCover = view.findViewById(R.id.imageView7);
    String url5 = "https://cn.yadingtour.com/templates/rt_denali/custom/images/2019_11/20191210153505.jpg";
    Glide.with(this).load(url5).into(imgCover);
    
    webInfo();
    return view;
    }

    private void webInfo() {
        WebApi webApi =NetworkFactory.getWebApi();
        Call<WebInfo> w =webApi.getWeb();
        w.enqueue(new Callback<WebInfo>() {
            @Override
            public void onResponse(Call<WebInfo> call, Response<WebInfo> response) {
                WebInfo webInfo =response.body();
                Log.d(TAG,"网页信息1" + webInfo.getText());

                Message message = new Message();
                message.what = 1;
                message.obj = webInfo;
                handler.sendMessage(message);
            }
            @Override
            public void onFailure(Call<WebInfo> call, Throwable t) {
            }
        });
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0){
                Toast.makeText(getActivity(),"访问错误，请重试",Toast.LENGTH_LONG).show();
            }else if(msg.what == 1){
                WebInfo webInfo = (WebInfo) msg.obj;

//                textView = textView.findViewById(R.id.textView);
//                textView.setText((webInfo.getText()));
//                textView = textView.findViewById(R.id.textView2);
//                textView.setText((webInfo.getText()));
                switch (textView.getId()){
                    case R.id.textView:
                        textView.setText(webInfo.getText());
                        break;
                    case R.id.textView2:
                        textView.setText(webInfo.getText());
                        break;
                    case R.id.textView3:
                        textView.setText(webInfo.getText());
                        break;
                    case R.id.textView4:
                        textView.setText(webInfo.getText());
                        break;
                    case R.id.textView5:
                        textView.setText(webInfo.getText());
                        break;
                    case R.id.textView6:
                        textView.setText(webInfo.getText());
                        break;
                }
            }
        }
    };
}